package com.example.restatr.Atcivity.VolunteerService;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.ContentAt;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.VolunteerService.Bean.VolunteerServiceBean;
import com.example.restatr.Atcivity.VolunteerService.Bean.VolunteerServiceNewsBean;
import com.example.restatr.Atcivity.VolunteerService.Bean.VolunteerServiceNewsInfoBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class VolunteerServiceAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private Banner rbt;
    private RecyclerView rv;
    private List<VolunteerServiceBean.RowsBean> rowsBeans = new ArrayList<>();
    private List<VolunteerServiceNewsBean.RowsBean> newsBeans = new ArrayList<>();
    private TabLayout tab;
    private int PagerNum = 1;
    private int type = 0;
    private adapter adapter = new adapter();
    private TextView bottomText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_volunteer_service_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        rbt = findViewById( R.id.banner );
        rv = findViewById( R.id.rv );
        tab = findViewById( R.id.tab );
        bottomText = findViewById( R.id.bottomText );

        Tools.SetBarTitle( toolbar, barTitle, "志愿活动", search, this, false );
        Tools.SetBanner("/prod-api/api/volunteer-service/ad-banner/list", MainActivity.sp.getString( "token", "" ), "data",
                "imgUrl",rbt,this );
        GetService();
        tab.addOnTabSelectedListener( new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().toString().equals( "活动" )) {
                    type = 0;
                    if (rowsBeans.size() > 0) {
                        rv.setAdapter( adapter );
                    } else {
                        GetService();
                    }
                    bottomText.setVisibility( View.VISIBLE );
                } else {
                    type = 1;
                    if (newsBeans.size() > 0) {
                        rv.setAdapter( new adapter() );
                    } else {
                        GetNews();
                    }
                    bottomText.setVisibility( View.GONE );
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        } );
        rv.addOnScrollListener( new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged( recyclerView, newState );
                if (type == 0) {
                    if (!recyclerView.canScrollVertically( 1 ) && newState == 0) {
                        if (PagerNum < 5) {
                            PagerNum++;
                            GetService();
                        } else {
                            bottomText.setText( "——到底了——" );
                        }

                    }
                }

            }
        } );
    }

    //活动报名
    private void PostService(int activityId, boolean newState) {
        new Thread( () -> {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put( "activityId", activityId );
                jsonObject.put( "newState", newState );
                String token = Tools.Post( "/prod-api/api/volunteer-service/register", MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                JSONObject jx = new JSONObject( token );
                String msg = jx.getString( "msg" );
                int code = jx.getInt( "code" );
                runOnUiThread( () -> {
                    Toast.makeText( this, msg, Toast.LENGTH_SHORT ).show();

                } );

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } ).start();
    }

    //获取新闻数据
    private void GetNews() {

        new Thread( () -> {
            String toke = Tools.Get( "/prod-api/api/volunteer-service/news/list", MainActivity.sp.getString( "token", "" ) );
            VolunteerServiceNewsBean volunteerServiceNewsBean = new Gson().fromJson( toke, VolunteerServiceNewsBean.class );
            newsBeans.addAll( volunteerServiceNewsBean.getRows() );
            runOnUiThread( () -> {
                if (newsBeans.size() > 0) {
                    rv.setAdapter( new adapter() );
                }
            } );
        } ).start();
    }

    //获取活动数据
    private void GetService() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/volunteer-service/activity/list?pageSize=10&pageNum=" + PagerNum, MainActivity.sp.getString( "token", "" ) );
            VolunteerServiceBean volunteerServiceBean = new Gson().fromJson( token, VolunteerServiceBean.class );
            int total = volunteerServiceBean.getTotal();
            if (rowsBeans.size() < total) {
                rowsBeans.addAll( volunteerServiceBean.getRows() );
            }
            runOnUiThread( () -> {
                if (rowsBeans.size() > 0) {
                    rv.setAdapter( adapter );
                }
            } );
        } ).start();
    }

    //获取新闻详情
    private void GetNewsContent(int id, int pos) {
        new Thread( () -> {
            String toke = Tools.Get( "/prod-api/api/volunteer-service/news/" + id, MainActivity.sp.getString( "token", "" ) );
            VolunteerServiceNewsInfoBean bean = new Gson().fromJson( toke, VolunteerServiceNewsInfoBean.class );
            runOnUiThread( () -> {
                if (bean.getCode() == 200) {
                    Intent intent = new Intent( VolunteerServiceAt.this, ContentAt.class );
                    intent.putExtra( "content", bean.getData().getContent() );
                    intent.putExtra( "title", newsBeans.get( pos ).getTitle() );
                    intent.putExtra( "type", "default" );
                    startActivity( intent );
                }
            } );


        } ).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.adapterViewHolder> {
        @NonNull
        @Override
        public adapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.zfz_list_item, parent, false );
            return new adapterViewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull adapterViewHolder holder, int position) {
            if (type == 0) {
                holder.tupian.setVisibility( View.GONE );
                holder.name.setText( rowsBeans.get( position ).getTitle() );
                holder.diz.setText( rowsBeans.get( position ).getUndertaker() );
                holder.phone.setText( rowsBeans.get( position ).getRequireText() );
                holder.jg.setVisibility( View.GONE );
                holder.mj.setText( rowsBeans.get( position ).getDetail() );
                holder.cardBtn.setVisibility( View.VISIBLE );
                holder.cardBtn.setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PostService( rowsBeans.get( position ).getId(), true );
                    }
                } );
            } else {
                Glide.with( VolunteerServiceAt.this )
                        .load( Tools.ip + newsBeans.get( position ).getImgUrl() )
                        .transform( new RoundedCorners( 15 ) )
                        .into( holder.tupian );
                holder.name.setText( newsBeans.get( position ).getTitle() );
                holder.diz.setText( newsBeans.get( position ).getSummary() );
                holder.phone.setVisibility( View.GONE );
                holder.jg.setVisibility( View.GONE );
                holder.mj.setVisibility( View.GONE );
                holder.itemView.setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GetNewsContent( newsBeans.get( position ).getId(), position );

                    }
                } );
            }
        }

        @Override
        public int getItemCount() {
            if (type == 0) {
                return rowsBeans.size();
            } else {
                return newsBeans.size();
            }
        }

        class adapterViewHolder extends RecyclerView.ViewHolder {
            private RelativeLayout ddInfo;
            private ImageView tupian;
            private TextView name;
            private TextView diz;
            private TextView phone;
            private TextView mj;
            private TextView hux;
            private TextView jg;
            private CardView cardBtn;

            public adapterViewHolder(@NonNull View itemView) {
                super( itemView );
                ddInfo = itemView.findViewById( R.id.ddInfo );
                tupian = itemView.findViewById( R.id.tupian );
                name = itemView.findViewById( R.id.name );
                diz = itemView.findViewById( R.id.diz );
                phone = itemView.findViewById( R.id.phone );
                mj = itemView.findViewById( R.id.mj );
                hux = itemView.findViewById( R.id.hux );
                jg = itemView.findViewById( R.id.jg );
                cardBtn = itemView.findViewById( R.id.cardBtn );
            }
        }
    }



}